package reference;

import org.apache.http.client.config.RequestConfig;
import org.apache.http.config.SocketConfig;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;

import java.util.concurrent.TimeUnit;

/**
 * @Author hujian
 * @Date 2021/12/6 17:52
 * @Description description
 */
public class HttpUtils {

    public static CloseableHttpClient httpClient;

    static {
        httpClient = httpClient();
    }

    public static CloseableHttpClient httpClient() {
        PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
        connectionManager.setMaxTotal(1000);
        connectionManager.setDefaultMaxPerRoute(100);
        connectionManager.closeIdleConnections(10, TimeUnit.SECONDS);
        SocketConfig socket = SocketConfig.custom()
                .setSoTimeout(30000)
                .build();
        RequestConfig config = RequestConfig.custom()
                .setSocketTimeout(30000)
                .setConnectTimeout(30000)
                .setConnectionRequestTimeout(30000).build();
        return HttpClients.custom().setConnectionManager(connectionManager)
                .setMaxConnPerRoute(1000)
                .setMaxConnTotal(1000)
                .setDefaultRequestConfig(config)
                .setDefaultSocketConfig(socket)
                .build();
    }

    public static CloseableHttpClient getHttpClient() {
        return httpClient;
    }
}
